package com.zut.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zut.constant.MessageConstant;
import com.zut.entity.Result;
import com.zut.pojo.OrderSetting;
import com.zut.service.OrderSettingService;
import com.zut.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置
 *
 * @author sq
 * @create 2021-07-01-14:47
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    //文件上传，实现文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //使用POI解析表格数据，list是整个sheet表单数据，String[]是一行数据的集合
            List<String[]> list = POIUtils.readExcel(excelFile);
            //批量导入Excel数据
            List<OrderSetting> data = new ArrayList<>();
            for (String[] strings : list) {
                //获取第一个单元格数据预约日期
                String dateStr = strings[0];
                //获取第二个单元格数据可预约人数
                String numberStr = strings[1];
                //将字符串类型转换成日期类型
                Date orderDate = new Date(dateStr);
                //将字符串类型转换成整数类型
                Integer orderNumber = Integer.parseInt(numberStr);
                //将数据封装在orderSetting实体类中
                OrderSetting orderSetting = new OrderSetting(orderDate, orderNumber);
                //将转换后的数据添加到list集合中
                data.add(orderSetting);
            }
            //通过dubbo远程调用服务,实现Excel预约数据批量导入到数据库
            orderSettingService.add(data);
            //文件上传成功
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    //根据月份查询预约设置
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            //调用服务，根据月份查询预约设置
            List<Map> orderSettingList = orderSettingService.getOrderSettingByMonth(date);
            //根据月份查询预约设置成功
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            //根据月份查询预约设置失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //根据日期编辑可预约人数
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            //调用服务，根据日期编辑可预约人数
            orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //预约设置失败
            return new Result(true,MessageConstant.ORDERSETTING_FAIL);
        }
    }

}